package com.hqyj.finance.server.bill.financebill.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.finance.server.bill.financebill.entity.UserExpenses;
import com.hqyj.finance.server.bill.financebill.entity.UserIncome;
import com.hqyj.finance.server.bill.financebill.json.R;
import com.hqyj.finance.server.bill.financebill.service.UserIncomeService;
import com.hqyj.finance.server.bill.financebill.views.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("income")
@RestController
public class UserIncomeController {

    @Resource
    private UserIncomeService userIncomeService;

    @PostMapping("/")
    public R saveUserIncomeInfo(@RequestBody  UserIncome userIncome){
        try{
            if(userIncomeService.saveUserIncomeInfo(userIncome)){
                return R.success("新增用户收入信息成功！");
            }
            return R.fail("新增用户收入信息失败！");
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/pager")
    public R queryUserIncomePagerByUid(@RequestParam("uid")Integer uid,@RequestParam("currentPageNum") Integer currentPageNum){
        try{
            Page<UserIncome> page = userIncomeService.queryUserIncomePagerByUid(uid,currentPageNum);
            return R.success("查询用户收入分页数据成功！",page);
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/{incomesId}")
    public R queryUserIncomeInfoByExpensesId(@PathVariable("incomesId") Integer incomesId){
        try{
            UserIncome userIncome = userIncomeService.queryUserIncomeInfoByIncomesId(incomesId);
            return R.success("查询用户收入详情资料成功！",userIncome);
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("search")
    public R searchUserMonthIncomePager(@RequestParam("uid")Integer uid, @RequestParam("realName") String realName, @RequestParam("month")String month,@RequestParam("currentPageNum") Integer currentPageNum){
        try{
            UserIncomeMonthsView userIncomeMonthsView = userIncomeService.queryUserIncomeMonthsViewByUid(uid,realName,month,currentPageNum);
            return R.success("查询用户指定月份的收入详情成功！",userIncomeMonthsView);
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("export")
    public R exportUserMonthExpenseToExcelFile(@RequestParam("uid")Integer uid, @RequestParam("realName") String realName, @RequestParam("month")String month,@RequestParam("currentPageNum") Integer currentPageNum){
        try{
            UserIncomeMonthsView userIncomeMonthsView = userIncomeService.queryUserIncomeMonthsViewByUid(uid,realName,month,currentPageNum);
            String uploadSuccessFileUrl = userIncomeService.exportUserIncomeMonthViewAsExcel(userIncomeMonthsView);
            return R.success("导出用户收入明细excel文件成功！",uploadSuccessFileUrl);
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("echarts/{type}")
    public R getUserIncomeEchartsByType(@PathVariable("type") String type, @RequestParam("uid") Integer uid, @RequestParam("realName") String realName, @RequestParam("month") String month) {
        try {
            switch (type) {
                case "curve":
                    UserIncomeMonthsCharts userIncomeMonthsCharts = userIncomeService.queryUserIncomeMonthsChartsByUid(uid, realName, month);
                    return R.success("查询收入曲线图信息成功！", userIncomeMonthsCharts);
                //break;
                case "pie":
                    UserIncomeCatalogMonthsCharts userIncomeCatalogMonthsCharts = userIncomeService.queryUserIncomeCatalogMonthsCharts(uid, realName, month);
                    return R.success("查询收入饼状图信息成功！", userIncomeCatalogMonthsCharts);
                //break;
                default:
                    return R.success("查询Echarts图表信息成功！",null);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }
}
